package com.vne.skyemart.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.vne.skyemart.model.CartItem;
import com.vne.skyemart.service.CartItemService;

@Controller
@SessionAttributes
@RequestMapping("/cartitem")
public class CartItemController {

	@Autowired
	private CartItemService cartItemService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public ModelAndView saveCartItem(@ModelAttribute(" cartItem") CartItem cartItem,
			BindingResult result) {
		 cartItemService.addCartItem(cartItem);
		return new ModelAndView("redirect:/cartItem.html");
	}

	@RequestMapping(method = RequestMethod.GET)
	public String listCartItem(Model model) {
		model.addAttribute("cartItem",  cartItemService.listCartItem());

		return "cartItemList";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public ModelAndView addCartItem(@ModelAttribute("cartItem") CartItem cartItem,
			BindingResult result) {
		return new ModelAndView("addCartItem");
	}
}